package com.cms.recursoshumanos.component.dialog;

import java.util.Calendar;
import java.util.Date;

import com.example.frontendrecursoshumanos.R;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.utilities.Tool;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;

public class CustomDatePickerMonthYearDialog extends Dialog 
{

    private NumberPicker mSpinnerMonth;
    private NumberPicker mSpinnerYear;
    
    private String [] listado_meses;
    
    public interface DateDialogListener 
  	{
        public void ready(Date fecha_selec);
        public void cancelled();
    }

    private DateDialogListener mReadyListener;

    public CustomDatePickerMonthYearDialog(Context context, DateDialogListener readyListener) 
    {
        super(context);
        this.setTitle("Seleccione la Fecha de Consulta");
        mReadyListener = readyListener;
    }
    public void setListadoMeses()
    {
    	Calendar c1 = Calendar.getInstance();
		listado_meses = new String[12];
    	
    	for (int i = 0 ; i < 12; i++)
    	{
    		c1.set(Calendar.MONTH, i);
    		Date date = c1.getTime();
    		listado_meses[i] = Tool.getInstance().parseDateToString(date, 8);
    	}
    }
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Calendar c1 = Calendar.getInstance();
        setContentView(R.layout.elementos_spinner_date_mes_ano);
        setListadoMeses();
        
        mSpinnerYear = (NumberPicker) findViewById (R.id.npAnos);
        mSpinnerYear.setMinValue(Constant.MIN_DATE_YEAR_VALUE_CUSTOM);
        mSpinnerYear.setMaxValue(Constant.MAX_DATE_YEAR_VALUE_CUSTOM);
        mSpinnerYear.setValue(c1.get(Calendar.YEAR));
       

        mSpinnerMonth = (NumberPicker) findViewById (R.id.npMeses);
        mSpinnerMonth.setMinValue(0);
        mSpinnerMonth.setMaxValue(11);
        mSpinnerMonth.setDisplayedValues(listado_meses);
        mSpinnerMonth.setValue(c1.get(Calendar.MONTH));
        
        Button buttonOK = (Button) findViewById(R.id.dialogOK);
        Button buttonCancel = (Button) findViewById(R.id.dialogCancel);
        buttonOK.setOnClickListener(new android.view.View.OnClickListener()
        {
            public void onClick(View v) 
            {
            	int month = mSpinnerMonth.getValue();
                int year = mSpinnerYear.getValue();
                mReadyListener.ready(Tool.getInstance().getFechaFormato(1, month, year));
                CustomDatePickerMonthYearDialog.this.dismiss();
            }
        });
        buttonCancel.setOnClickListener(new android.view.View.OnClickListener(){
            public void onClick(View v) 
            {
                mReadyListener.cancelled();
                CustomDatePickerMonthYearDialog.this.dismiss();
            }
        });
    }
    
 

   
}